from functools import wraps

from flask import current_app,g
from flask import session




def host_news_filter(index):
    """首页热点数据的样式过滤器"""
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ""


def user_login_data(view_func):
    """
    自定义
    特点:装饰器会修改被装饰的函数的__name__属性，改成wrapper
    :param view_func:
    :return:
    """
    from info.models import User
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 将用户信息存在g变量中
        g.user = user
        return view_func(*args, **kwargs)

    return wrapper
